package me.firstapp.repository.shop.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import me.firstapp.module.shop.ShoppingCart;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.shop.ShoppingCartRepository;

@Repository
public class ShoppingCartRepositoryImpl extends EntityRepositoryImpl<ShoppingCart> implements ShoppingCartRepository {

	@Override
	protected Class<ShoppingCart> getPersistentClass() {
		return ShoppingCart.class;
	}

	@Override
	public ShoppingCart findMemberShoppingCart(Long memberId, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM ShoppingCart bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		sql.append(" AND bean.member.id=:memberId ");
		parameters.put("memberId", memberId);

		List<ShoppingCart> results = super.query(ShoppingCart.class, sql.toString(), parameters);
		ShoppingCart shoppingCart = results.isEmpty() ? null : results.get(0);
		return shoppingCart;
	}

	@Override
	public ShoppingCart findById(Long id, Long apiClientId) {
		StringBuilder sql = new StringBuilder(" FROM ShoppingCart bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);

		sql.append(" AND bean.id=:id ");
		parameters.put("id", id);

		List<ShoppingCart> results = super.query(ShoppingCart.class, sql.toString(), parameters);
		return results.isEmpty() ? null : results.get(0);
	}

}
